package com.ljy.springcloud.controller;

import com.ljy.springcloud.entities.CommonResult;
import com.ljy.springcloud.entities.Payment;
import com.ljy.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
public class PaymentController {
    @Autowired
    PaymentService paymentService;
    @Resource
    DiscoveryClient discoveryClient;

    @PostMapping("/payment/insert")
    public CommonResult insert(@RequestBody Payment payment) {
        int insert = paymentService.insert(payment);
        log.info("===插入结果：{}", insert);
        if (insert > 0) {
            return new CommonResult(200, "插入成功-8002");
        }
        return new CommonResult(444, "插入失败-8002");
    }

    @GetMapping("/payment/find/{id}")
    public CommonResult<Payment> find(@PathVariable(name = "id") int id) {
        log.info("===id:{}", id);
        Payment payment = paymentService.find(id);
        if (payment != null) {
            return new CommonResult<>(200, "查询成功-8002", payment);
        }
        return new CommonResult<>(444, "未查到-8002", null);
    }

    @GetMapping("/payment/discovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("===service:{}", service);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance instance : instances) {
            log.info("id:{}  host:{}  port:{}  uri:{}", instance.getInstanceId(), instance.getHost(), instance.getPort(), instance.getUri());
        }
        return instances;
        /*[{"host":"192.168.31.159","port":8002,
        "metadata":{"management.port":"8002"},"instanceId":"payment8001",
        "uri":"http://192.168.31.159:8002","secure":false,
        "serviceId":"CLOUD-PAYMENT-SERVICE",
        "instanceInfo":{"instanceId":"payment8001",
        "app":"CLOUD-PAYMENT-SERVICE",
        "appGroupName":null,"ipAddr":"192.168.31.159","sid":"na",
        "homePageUrl":"http://192.168.31.159:8002/",
        "statusPageUrl":"http://192.168.31.159:8002/actuator/info",
        "healthCheckUrl":"http://192.168.31.159:8002/actuator/health",
        "secureHealthCheckUrl":null,"vipAddress":"cloud-payment-service",
        "secureVipAddress":"cloud-payment-service","countryId":1,
        "dataCenterInfo":{"@class":"com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo","name":"MyOwn"},
        "hostName":"192.168.31.159","status":"UP","overriddenStatus":"UNKNOWN",
        "leaseInfo":{"renewalIntervalInSecs":30,"durationInSecs":90,
            "registrationTimestamp":1638006245342,"lastRenewalTimestamp":1638006245342,
            "evictionTimestamp":0,"serviceUpTimestamp":1638006244879},
        "isCoordinatingDiscoveryServer":false,
        "metadata":{"management.port":"8002"},"lastUpdatedTimestamp":1638006245342,
        "lastDirtyTimestamp":1638006245329,"actionType":"ADDED","asgName":null},"scheme":null}]*/
    }
}
